/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JWindow;

/**
 *
 * @author Administrator
 */
public class NetworkUtilities {

    public static byte[] getLocalIPAddress() throws UnknownHostException{
        InetAddress addr = InetAddress.getLocalHost();
        byte[] ipAddr = addr.getAddress();
        return ipAddr;
    }

    public static String getLocalHostName() throws UnknownHostException{
        InetAddress addr = InetAddress.getLocalHost();
        byte[] ipAddr = addr.getAddress();
        String hostname = addr.getHostName();
        return hostname;
    }

    public static int getSubnetMask() throws UnknownHostException, SocketException{
        InetAddress localHost = Inet4Address.getLocalHost();
        NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
        return networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();
    }

    public static String getGateway() throws IOException{
        Process result = Runtime.getRuntime().exec("netstat -rn");

        BufferedReader output = new BufferedReader
        (new InputStreamReader(result.getInputStream()));

        String line = output.readLine();

        while(line != null){
            if ( line.startsWith("Default") == true )
                break;
            line = output.readLine();
        }
        System.out.println(line);
        StringTokenizer st = new StringTokenizer( line );
        st.nextToken();
        st.nextToken();
        String adapter = st.nextToken();
        return adapter;

    }

    public static void main(String[] args){
     JFrame f = new JFrame("JFrame");
     f.setUndecorated(true);
     f.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
     f.setSize(400,300);
     f.setLocationRelativeTo(null);
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.setVisible(true);
    }


}
